package com.mapforge.gui;

import java.awt.Color;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.TransferHandler;

public class TransferablePanel extends JPanel implements Transferable {
	/**
	 * Default serial version ID, here to remove warnings.
	 */
	private static final long serialVersionUID = 1L;
	/**
	 * Whether or not this panel is considered selected.
	 */
	private boolean selected;

	private Color selectedColor;
	private Color unselectedColor;

	// private MouseAdapter mouseListener;

	public TransferablePanel() {
		super();
		this.setTransferHandler(new PanelTransferHandler());
		unselectedColor = getBackground();
		selectedColor = new Color(50, 150, 255);
	}

	public boolean isSelected() {
		return selected;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
		if (selected)
			setBackground(selectedColor);
		else
			setBackground(unselectedColor);
	}

	public Object getTransferData(DataFlavor flavor)
			throws UnsupportedFlavorException, IOException {
		return this;
	}

	public DataFlavor[] getTransferDataFlavors() {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isDataFlavorSupported(DataFlavor dataFlavor) {
		return dataFlavor.getRepresentationClass() == TransferablePanel.class;
	}

	private class PanelTransferHandler extends TransferHandler {
		private static final long serialVersionUID = 1L;

		public Transferable createTransferable(JComponent c) {
			if (c instanceof TransferablePanel) {
				TransferablePanel tip = (TransferablePanel) c;
				return tip;
			} else
				return null;
		}

		public int getSourceActions(JComponent c) {
			if (c instanceof TransferablePanel) {
				return TransferHandler.COPY;
			}
			return TransferHandler.NONE;
		}
	}
}
